package com.zan.controller;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.zan.config.MyPayConfig;
import com.zan.entity.Orders;
import com.zan.service.OrderService;
import com.zan.vo.ResStatus;
import com.zan.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/order")
@Api(value = "提供订单相关的接口", tags = "订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/add")
    @ApiOperation("添加订单接口")
    public ResultVo addOrder(String cids, @RequestBody Orders orders) {
        ResultVo resultVo = null;
        try {
            Map<String, String> orderInfo = orderService.addOrder(cids, orders);
            String orderId = orderInfo.get("orderId");
            if (orderId != null) {
                //设置当前订单的信息
                HashMap<String, String> data = new HashMap<>();
                data.put("body", orderInfo.get("productName")); //商品描述
                data.put("out_trade_no", orderId); //交易号码: 使用当前的用户订单的编号作为当前支付交易的编号
                data.put("fee_type", "CNY"); //支付币种
//                data.put("total_fee", orders.getActualAmount() * 100 + ""); //支付总金额
                data.put("total_fee", "1");
                data.put("trade_type", "NATIVE"); //交易类型
//                data.put("notify_url", "http://zan.vipgz1.91tunnel.com/pay/callback"); //支付成功回调的方法
                //部署到服务器上，就不用内网穿透了，直接用公网ip
                data.put("notify_url", "http://47.113.207.152:8080/pay/callback"); //支付成功回调的方法

                //申请支付链接：微信支付、支付宝支付 【wx支付】
                WXPay wxPay = new WXPay(new MyPayConfig());

                //发送请求获取响应
                Map<String, String> resp = wxPay.unifiedOrder(data);
                orderInfo.put("payUrl", resp.get("code_url"));
//                System.out.println(resp);
                resultVo = new ResultVo(ResStatus.OK.code, "提交订单成功", orderInfo);
            } else {
                resultVo = new ResultVo(ResStatus.NO.code, "提交订单失败！", null);
            }
        } catch (SQLException e) {
            resultVo = new ResultVo(ResStatus.NO.code, "提交订单失败！", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultVo;
    }

    @GetMapping("/status/{oid}")
    public ResultVo getOrderById(@PathVariable("oid") String orderId, @RequestHeader("token") String token) {
        return orderService.getOrderById(orderId);
    }

    @GetMapping("/list")
    @ApiOperation("订单列表查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(dataType = "String", name = "status", value = "订单状态", required = false),
            @ApiImplicitParam(dataType = "int", name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(dataType = "int", name = "limit", value = "每页条数", required = true)
    })
    public ResultVo list(@RequestHeader("token") String token, String userId, String status, int pageNum, int limit) {
        return orderService.listOrders(userId, status, pageNum, limit);
    }

    @GetMapping("/list2")
    @ApiOperation("订单列表查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(dataType = "String", name = "status", value = "订单状态", required = false),
            @ApiImplicitParam(dataType = "int", name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(dataType = "int", name = "limit", value = "每页条数", required = true)
    })
    public ResultVo list2(@RequestHeader("token") String token, String userId, String status, int pageNum, int limit) {
        return orderService.listOrders2(userId, status, pageNum, limit);
    }

}
